package com.example.hoasbackend.web.controller;

import com.example.hoasbackend.model.AO.HotelAO;
import com.example.hoasbackend.model.VO.HotelInfoVO;
import com.example.hoasbackend.model.domain.RoomType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.hoasbackend.common.JsonResponse;
import com.example.hoasbackend.service.HotelService;
import com.example.hoasbackend.model.domain.Hotel;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lhx
 * @since 2022-03-03
 * @version v1.0
 */
@Controller
@RequestMapping("/api/hotel")
public class HotelController {

    private final Logger logger = LoggerFactory.getLogger( HotelController.class );

    @Autowired
    private HotelService hotelService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@RequestParam("id") Long id)throws Exception {
        Hotel  hotel =  hotelService.getById(id);
        return JsonResponse.success(hotel);
    }



    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@RequestParam("id") Long id) throws Exception {
        hotelService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateHotel(@RequestParam("id") Long  id,Hotel  hotel) throws Exception {
        hotel.setId(id);
        hotelService.updateById(hotel);
        return JsonResponse.success(null);
    }

    /**
     * 描述:创建Hotel
     *
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(Hotel  hotel) throws Exception {
        hotelService.save(hotel);
        return JsonResponse.success(null);
    }

    /**
     * 描述:多条件查询hotel
     *
     */
    @RequestMapping(path = "/search/filter", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse searchByMultipleConditions(@RequestBody HotelAO hotelAO) throws Exception {
        List<HotelInfoVO> hotels=hotelService.listByMultipleConditions(hotelAO);
        if(hotels!=null&&hotels.size()>0)
        return JsonResponse.success(hotels);
        return JsonResponse.failure("InvalidHotel.NotFound").setCode(404);
    }

    /**
     * 描述:查询附近hotel
     *
     */
    @RequestMapping(path = "/search/position", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse searchByPosition(@RequestParam("lat")Double lat,@RequestParam("lng")Double lng) throws Exception {
        List<HotelInfoVO> hotels=hotelService.listByPosition(lat,lng);
        if(hotels!=null&&hotels.size()>0)
        return JsonResponse.success(hotels);
        return JsonResponse.failure("InvalidHotel.NotFound").setCode(404);
    }

    /**
     * 描述:查询酒店房间类型
     *
     */
    @RequestMapping(path = "/search_room", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse searchRooms(@RequestParam("id")Long id) throws Exception {
        List<RoomType> rooms=hotelService.listAllRoom(id);
        if(rooms!=null&&rooms.size()>0)
        return JsonResponse.success(rooms);
        return JsonResponse.failure("InvalidRooms.NotFound").setCode(404);
    }

    /**
     * 描述：根据Id 查询酒店所有信息，包括评论数量距离等
     *
     */
    @RequestMapping(path = "/gethotel",method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getHotelVOById(@RequestParam("id") Long id)throws Exception {
        HotelInfoVO hotel=hotelService.getHotelVOById(id);
        if(hotel!=null)
            return JsonResponse.success(hotel);
        return JsonResponse.failure("InvalidHotel.NotFound").setCode(404);
    }

}

